package com.gitee.huxing.courese.core;

import java.io.Serializable;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.courese.core
 * @ClassName: KmqConsumer
 * @Author: huxing
 * @DateTime: 2021-11-07 4:06 下午
 */
public class KmqConsumer<T> implements Serializable {

    private static final long serialVersionUID = 2624785427149500292L;

    private final KmqBroker broker;

    private Kmq kmq;

    public KmqConsumer(KmqBroker broker) {
        this.broker = broker;
    }

    /**
     * @Description: 消息订阅
     * @Author: huxing
     * @param topic
     * @Date: 2021/11/7 6:31 下午
     **/
    public void subscribe(String topic){
        this.kmq = this.broker.findKmq(topic);
        if (null == kmq) {
            throw new RuntimeException("Topic[" + topic + "] doesn't exist.");
        }
    }

    /**
     * @Description: 获取消息
     * @Author: huxing
     * @param timeout
     * @return com.gitee.huxing.courese.core.KmqMessage<T>
     * @Date: 2021/11/7 6:31 下午
     **/
    public KmqMessage<T> poll(long timeout){
        return kmq.poll(this.hashCode(), timeout);
    }
}
